﻿using System;
using System.IO;
using System.Text;
using System.Collections;
using System.Data; 

namespace WinnerCMS.Common.Utilities
{
    /// <summary>
    /// 导出PDF
    /// </summary>
    public class PdfGenerator
    {
        private const float pageWidth = 594.0f;
        private const float pageDepth = 828.0f;
        private const float pageMargin = 30.0f;
        private const float fontSize = 20.0f;
        private const float leadSize = 10.0f;
        static readonly StreamWriter pPDF = new StreamWriter("E:\\myPDF.pdf");
        static readonly MemoryStream mPDF = new MemoryStream();
        private void ConvertToByteAndAddtoStream(string strMsg)
        {
            Byte[] buffer = null;
            buffer = Encoding.ASCII.GetBytes(strMsg);
            mPDF.Write(buffer, 0, buffer.Length);
        }

        private string xRefFormatting(long xValue)
        {
            string strMsg = xValue.ToString();
            int iLen = strMsg.Length;
            if (iLen < 10)
            {
                StringBuilder s = new StringBuilder();
                int i = 10 - iLen;
                s.Append('0', i);
                strMsg = s.ToString() + strMsg;
            }
            return strMsg;
        }

        public void ExportPdf(DataTable dt)
        {
            ArrayList xRefs = new ArrayList();
            //Byte[] buffer=null; 
            float yPos = 0f;
            long streamStart = 0;
            long streamEnd = 0;
            long streamLen = 0;
            string strPdfMessage = null;
            //PDF文档头信息 
            strPdfMessage = "%PDF-1.1\n";
            ConvertToByteAndAddtoStream(strPdfMessage);
            xRefs.Add(mPDF.Length);
            strPdfMessage = "1 0 obj\n";
            ConvertToByteAndAddtoStream(strPdfMessage);
            strPdfMessage = " < < /Length 2 0 R >>\n";
            ConvertToByteAndAddtoStream(strPdfMessage);
            strPdfMessage = "stream\n";
            ConvertToByteAndAddtoStream(strPdfMessage);
            ////////PDF文档描述 
            streamStart = mPDF.Length;
            //字体 
            strPdfMessage = "BT\n/F0 " + fontSize + " Tf\n";
            ConvertToByteAndAddtoStream(strPdfMessage);
            //PDF文档实体高度 
            yPos = pageDepth - pageMargin;
            strPdfMessage = pageMargin + " " + yPos + " Td\n";
            ConvertToByteAndAddtoStream(strPdfMessage);
            strPdfMessage = leadSize + " TL\n";
            ConvertToByteAndAddtoStream(strPdfMessage);

            //实体内容 
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    strPdfMessage = "(" + dt.Rows[i][j].ToString() + ")Tj\n" + "\n";
                    ConvertToByteAndAddtoStream(strPdfMessage);
                }
            }
            strPdfMessage = "ET\n";
            ConvertToByteAndAddtoStream(strPdfMessage);
            streamEnd = mPDF.Length;

            streamLen = streamEnd - streamStart;
            strPdfMessage = "endstream\nendobj\n";
            ConvertToByteAndAddtoStream(strPdfMessage);
            //PDF文档的版本信息 
            xRefs.Add(mPDF.Length);
            strPdfMessage = "2 0 obj\n" + streamLen + "\nendobj\n";
            ConvertToByteAndAddtoStream(strPdfMessage);

            xRefs.Add(mPDF.Length);
            strPdfMessage = "3 0 obj\n < </Type/Page/Parent 4 0 R/Contents 1 0 R>>\nendobj\n";
            ConvertToByteAndAddtoStream(strPdfMessage);

            xRefs.Add(mPDF.Length);
            strPdfMessage = "4 0 obj\n < </Type /Pages /Count 1\n";
            ConvertToByteAndAddtoStream(strPdfMessage);
            strPdfMessage = "/Kids[\n3 0 R\n]\n";
            ConvertToByteAndAddtoStream(strPdfMessage);
            strPdfMessage = "/Resources < </ProcSet[/PDF/Text]/Font < </F0 5 0 R>> >>\n";
            ConvertToByteAndAddtoStream(strPdfMessage);
            strPdfMessage = "/MediaBox [ 0 0 " + pageWidth + " " + pageDepth + " ]\n>>\nendobj\n";
            ConvertToByteAndAddtoStream(strPdfMessage);

            xRefs.Add(mPDF.Length);
            strPdfMessage = "5 0 obj\n < </Type/Font/Subtype/Type1/BaseFont/Courier/Encoding/WinAnsiEncoding>>\nendobj\n";
            ConvertToByteAndAddtoStream(strPdfMessage);

            xRefs.Add(mPDF.Length);
            strPdfMessage = "6 0 obj\n < </Type/Catalog/Pages 4 0 R>>\nendobj\n";
            ConvertToByteAndAddtoStream(strPdfMessage);

            streamStart = mPDF.Length;
            strPdfMessage = "xref\n0 7\n0000000000 65535 f \n";
            for (int i = 0; i < xRefs.Count; i++)
            {
                strPdfMessage += xRefFormatting((long)xRefs[i]) + " 00000 n \n";
            }
            ConvertToByteAndAddtoStream(strPdfMessage);
            strPdfMessage = "trailer\n < <\n/Size " + (xRefs.Count + 1) + "\n/Root 6 0 R\n>>\n";
            ConvertToByteAndAddtoStream(strPdfMessage);

            strPdfMessage = "startxref\n" + streamStart + "\n%%EOF\n";
            ConvertToByteAndAddtoStream(strPdfMessage);
            mPDF.WriteTo(pPDF.BaseStream);
            //System.Web.HttpContext.Current.Response.Write(pPDF.BaseStream); 
            mPDF.Close();
            pPDF.Close();
        }

    }
}
